/*
 *请求模块
*/
import axios from 'axios'
import store from '@/store'
import JSONBig from 'json-bigint'
const request = axios.create({
  // baseURL: 'http://api.research.itcast.cn/' // 接口的基准路径
  baseURL: 'http://toutiao-app.itheima.net',
  transformResponse: [function (data) {
    try {
      return JSONBig.parse(data)
    } catch (err) {
      return data
    }
  }]
})
// 请求拦截器
request.interceptors.request.use(
  function (config) {
    // Do something before request is sent
    // config ：本次请求的配置对象
    // config 里面有一个属性：headers
    const { user } = store.state
    if (user) {
      config.headers.Authorization = `Bearer ${user}`
    }
    return config
  }, function (error) {
    // Do something with request error
    return Promise.reject(error)
  }
)
// 响应拦截器

export default request
